package com.climbingGuide.java;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TwoLineListItem;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

public class InfoSector extends Activity{
	private BaseDatos db3;
	public ActionBar actionBar;
	int[] fotos;
	ListView listaVias;
	ImageView foto;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sector_info);
		
		Bundle datos = getIntent().getExtras();	
		String sectorName = datos.getString("sectorName");
        actionBar = (ActionBar)findViewById(R.id.actionbar3);
        actionBar.setHomeAction(new CloseAction(android.R.drawable.ic_dialog_map));
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(sectorName);
        actionBar.setProgressBarVisibility(View.VISIBLE);
        
        db3 = new BaseDatos(this);
   	 	db3.abrirBD();
		
		String selection = "NombreSec LIKE '" +sectorName+"'";
		//String orderBy = "ID DESC";
		Cursor c = db3.getInfoVias(true, new String[]{"foto"}, selection, null, null, null, null);
		//Aqui sacamos el vector de fotos de la gallery
		int valor = c.getCount();
		if (valor != 0){
			fotos = new int[valor];
			int i = 0;
			String nombrePack = getPackageName();
			while(c.moveToNext()){
				String[] vector = c.getString(0).split("\\.");
				String rutaFoto = "drawable/" + vector[0];
				//Cargamos el ID del drawable por el nombre
				fotos[i] = getResources().getIdentifier(rutaFoto, null, nombrePack);
				i++;
			}
		}
		c.close();
		
		foto = new ImageView(InfoSector.this);
		Gallery galeria = (Gallery)findViewById(R.id.galeria);
		listaVias = (ListView)findViewById(R.id.ListaVias);
		listaVias.addHeaderView(foto);
		galeria.setAdapter(new ImageAdapter(this));
		galeria.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				foto.setImageResource(fotos[arg2]);
				String nombreFoto = getResources().getResourceEntryName(fotos[arg2]);
				String selection = "foto LIKE '" +nombreFoto+".jpg'";
				String orderBy = "ID DESC";	
				
				Cursor c = db3.getInfoVias(false, new String[]{"ID", "NombreVia", "Grado", "Observaciones"}, selection, null, null, null, orderBy);
				//int numCol = c.getCount();
				final String[] datos = new String[c.getCount()];
				int i = 0;
				
				while(c.moveToNext()){
					String texto = "";
					int valor = c.getColumnCount();
					for (int j = 0; j < valor; j++){
						 texto +=  c.getString(j) +"|";
					}
					datos[i] = texto;
					i++;
				}
				
				ArrayAdapter<String> adaptador = new ArrayAdapter<String>(InfoSector.this, android.R.layout.simple_list_item_2,datos){
					@Override
					public View getView(int position, View convertView, ViewGroup parent){
						TwoLineListItem fila;
						String[] tittle;
						if (convertView == null){
							LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
							fila = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);
						}else
							fila = (TwoLineListItem)convertView;
						
						tittle = datos[position].split("\\|");
						fila.getText1().setText(tittle[0]+ ". "+ tittle[1]+ " | "+tittle[2]);
						fila.getText2().setText(tittle[3]);
						return fila;
					}
				};
				listaVias.setAdapter(adaptador);
				c.close();
			}
		});
		actionBar.setProgressBarVisibility(View.GONE);
	}
	@Override
	public void onDestroy(){
		db3.close();
		super.onDestroy();
	}

	private class CloseAction extends AbstractAction {

		public CloseAction(int arg0) {
			super(arg0);
		}
        @Override
        public void performAction(View view) {
    
            //Toast.makeText(ClimbingGuideActivity.this,"texto", Toast.LENGTH_SHORT).show();
        	finish();
        }
    }
	public class ImageAdapter extends BaseAdapter{
		private Context ctx;
		int imageBackground;
		
		public ImageAdapter(Context contexto){
			ctx = contexto;
			TypedArray attr = ctx.obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = attr.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
			attr.recycle();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fotos.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ImageView imagen = (ImageView)arg1;
			if (imagen == null){
				imagen = new ImageView(ctx);
				imagen.setLayoutParams(new Gallery.LayoutParams(150, 100));
				imagen.setScaleType(ImageView.ScaleType.FIT_XY);
				imagen.setBackgroundResource(imageBackground);
				
			}
			imagen.setImageResource(fotos[arg0]);
			return imagen;
		}
	}
}